package com.sduept.nwld.dataserver.manager.statistics;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.model.countanalyse.FaultFastRemove;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCorrectAct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProtectKpiCountManager {

	@Autowired
	private SuperDAO dao;
	@Autowired
	private StatisticsCommonManager commonM;
	// 统计年平均指标
	private static final String YEAR_COUNT_ACT_SQL = "SELECT s.stationId,CAST(SUM(s.protectRefuseActionCount) AS signed) as "
			+ "protectRefuseActionCount,CAST(SUM(s.protectRightActionCount) AS signed) as protectRightActionCount,"
			+ "CAST(SUM(s.protectUnwantedActionCount) AS signed) as protectUnwantedActionCount, CAST(DATE_FORMAT(f.faultTime,'%Y') AS signed) "
			+ "AS year,f.voltage FROM t_cim_fault f LEFT JOIN t_fault_report_main m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s "
			+ "ON m.id=s.pid WHERE s.stationId IS NOT NULL AND f.faultTime IS NOT NULL AND f.voltage=? AND f.faultTime BETWEEN ? AND ? AND "
			+ "f.status=0 AND s.protectRefuseActionCount IS NOT NULL AND s.protectRightActionCount IS NOT NULL AND s.protectUnwantedActionCount IS "
			+ "NOT NULL GROUP BY s.stationId";
	// 统计每个月的平均指标
	private static final String MONTH_COUNT_ACT_SQL = "SELECT s.stationId,CAST(SUM(s.protectRefuseActionCount) AS signed) "
			+ "as protectRefuseActionCount,CAST(SUM(s.protectRightActionCount) AS signed) as protectRightActionCount,"
			+ "CAST(SUM(s.protectUnwantedActionCount) AS signed) as protectUnwantedActionCount, CAST(DATE_FORMAT(f.faultTime,'%Y') "
			+ "AS signed) AS year, CAST(DATE_FORMAT(f.faultTime,'%m') AS signed) AS month,f.voltage FROM t_cim_fault f LEFT JOIN "
			+ "t_fault_report_main m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s ON m.id=s.pid WHERE s.stationId "
			+ "IS NOT NULL AND f.faultTime IS NOT NULL AND f.voltage=? AND f.faultTime BETWEEN ? AND ? AND f.status=0 AND s.protectRefuseActionCount "
			+ "IS NOT NULL AND s.protectRightActionCount IS NOT NULL AND s.protectUnwantedActionCount IS NOT NULL GROUP BY "
			+ "s.stationId,year,month";
	// 统计全部
	private static final String ALL_COUNT_ACT_SQL = "SELECT CAST(DATE_FORMAT(f.faultTime,'%Y') AS signed AS year, "
			+ "CAST(DATE_FORMAT(f.faultTime,'%m') AS signed) AS month, s.stationId, f.voltage, CAST(SUM(s.protectRefuseActionCount) "
			+ "AS signed) AS protectRefuseActionCount,CAST(SUM(s.protectRightActionCount) AS signed) AS protectRightActionCount,"
			+ "CAST(SUM(s.protectUnwantedActionCount) AS signed) AS protectUnwantedActionCount,f.voltage FROM "
			+ "t_cim_fault f LEFT JOIN t_fault_report_main m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s ON m.id=s.pid "
			+ "WHERE s.stationId IS NOT NULL AND f.faultTime IS NOT NULL AND f.voltage IS NOT NULL AND f.voltage!='' AND f.status=0 AND "
			+ "s.protectRefuseActionCount IS NOT NULL AND s.protectRightActionCount IS NOT NULL AND s.protectUnwantedActionCount "
			+ "IS NOT NULL GROUP BY stationId,year,month";
	// 统计年平均指标
	private static final String YEAR_COUNT_REMOVE_SQL = "SELECT s.stationId,CAST(SUM(s.rapidActionReality) AS signed) AS "
			+ "rapidActionReality, CAST(SUM(s.rapidProtectionShould) AS signed) AS rapidProtectionShould, "
			+ "CAST(DATE_FORMAT(f.faultTime,'%Y') AS signed) AS year,f.voltage FROM t_cim_fault f LEFT JOIN t_fault_report_main "
			+ "m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s ON m.id=s.pid WHERE s.stationId IS NOT NULL AND "
			+ "f.faultTime IS NOT NULL AND f.voltage=? AND f.faultTime BETWEEN ? AND ? AND f.status=0 AND s.rapidActionReality IS NOT NULL "
			+ "AND s.rapidProtectionShould IS NOT NULL GROUP BY s.stationId";
	// 统计每个月的平均指标
	private static final String MONTH_COUNT_REMOVE_SQL = "SELECT s.stationId,CAST(SUM(s.rapidActionReality) AS signed) AS "
			+ "rapidActionReality,CAST(SUM(s.rapidProtectionShould) AS signed) AS rapidProtectionShould,"
			+ "CAST(DATE_FORMAT(f.faultTime,'%Y') AS signed) AS year,CAST(DATE_FORMAT(f.faultTime,'%m') AS signed) AS month,"
			+ "f.voltage FROM t_cim_fault f LEFT JOIN t_fault_report_main m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s "
			+ "ON m.id=s.pid WHERE s.stationId IS NOT NULL AND f.faultTime IS NOT NULL AND f.voltage=? AND f.faultTime BETWEEN ? AND ? "
			+ "AND f.status=0 AND s.rapidActionReality IS NOT NULL AND s.rapidProtectionShould IS NOT NULL GROUP BY s.stationId,year,month";
	private static final String ALL_COUNT_REMOVE_SQL = "SELECT CAST(DATE_FORMAT(f.faultTime,'%Y') AS signed) AS year, "
			+ "CAST(DATE_FORMAT(f.faultTime,'%m') AS signed) AS month, s.stationId, f.voltage, CAST(SUM(s.rapidActionReality) AS signed) "
			+ "AS rapidActionReality,CAST(SUM(s.rapidProtectionShould) AS signed) AS rapidProtectionShould,f.voltage FROM t_cim_fault f "
			+ "LEFT JOIN t_fault_report_main m ON f.id = m.frid LEFT JOIN t_fault_report_station_detail s ON m.id = s.pid WHERE "
			+ "s.stationId IS NOT NULL AND f.faultTime IS NOT NULL AND f.voltage!='' AND f.voltage IS NOT NULL AND f.status=0 AND s.rapidActionReality "
			+ "IS NOT NULL AND s.rapidProtectionShould IS NOT NULL GROUP BY stationId,year,month";

	private DecimalFormat df = new DecimalFormat("#0.00");
	/**
	 * 根据年、月、电压等级统计保护正确动作率
	 * @param year
	 * @param month
	 */
	@Deprecated
	public void countProtectCorrectAct(Integer year,Integer month,Integer voltage) {
		List<ProtectCorrectAct> actlist = new ArrayList<ProtectCorrectAct>();
		String sql = (month==null?YEAR_COUNT_ACT_SQL:MONTH_COUNT_ACT_SQL);
		List<?> list = commonM.getListByCondition(year,month,voltage,sql);
		Map<String, List<Object>> companyMap = commonM.getCompanyMap(list, 0);
		for (String company : companyMap.keySet()) {
			List<Object> compList = companyMap.get(company);
			if(null != compList) {
				int totalActSum = 0;
				int unwantedActSum = 0;
				for (Object obj : compList) {
					Object[] objs = (Object[])obj;
					int protectRefuseActionCount = 0;
					int protectRightActionCount = 0;
					int protectUnwantedActionCount = 0;
					if(null != objs[1]) {
						//						BigDecimal ss = (BigDecimal)objs[1];
						protectRefuseActionCount = objectTransToInteger(objs[1]);
					}
					if(null != objs[2]) {
						//						BigDecimal ss = (BigDecimal)objs[2];
						protectRightActionCount = objectTransToInteger(objs[2]);
					}
					if(null != objs[3]) {
						//						BigDecimal ss = (BigDecimal)objs[3];
						protectUnwantedActionCount = objectTransToInteger(objs[3]);
					}
					int sum = protectRefuseActionCount+protectRightActionCount+protectUnwantedActionCount;
					totalActSum += sum;
					unwantedActSum += protectUnwantedActionCount;
				}
				ProtectCorrectAct act = new ProtectCorrectAct();
				act.setId(ObjectId.get().toString());
				act.setYear(year);
				act.setMonth(month);
				act.setVoltage(voltage);
				act.setPlan(100f);
				act.setActNum(totalActSum);
				act.setUnwantedActNum(unwantedActSum);
				act.setStatus(0);
				if(totalActSum!=0) {
					String reality = df.format((float)(totalActSum-unwantedActSum)/totalActSum*100);
					act.setReality(Float.parseFloat(reality));
				} else {
					act.setReality(100f);
				}
				act.setSubCompany(company);
				actlist.add(act);
			}
		}
		try {
			dao.persistentBatch(ProtectCorrectAct.class, actlist);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据年、月、电压等级、单位类型、是否覆盖统计保护正确动作率
	 * @param year
	 * @param month
	 */
	public void countProtectCorrectAct(Integer year, Integer month, Integer voltage, int unitType, boolean covered) {
		List<ProtectCorrectAct> actList = new ArrayList<ProtectCorrectAct>();
		String sql = (month==null?YEAR_COUNT_ACT_SQL:MONTH_COUNT_ACT_SQL);
		List<ProtectCorrectActInner> list = commonM.getProtectByConditions(year,month,voltage,sql);
		if(unitType == 0) {
			Map<String, List<ProtectCorrectActInner>> maintenanceUnitMap = commonM.getProtectMaintenanceUnitMap(list);//获取检修单位对应的数据map
			List<ProtectCorrectAct> maintenanceList = countProtectCorrectAct(maintenanceUnitMap, 0, year,  month,  voltage);
			actList.addAll(maintenanceList);
		}else {
			Map<String, List<ProtectCorrectActInner>> dispatchUnitMap = commonM.getProtectDispatchUnitMap(list);//获取调管单位对应的数据map
			List<ProtectCorrectAct> dispatchList = countProtectCorrectAct(dispatchUnitMap, 1, year,  month,  voltage);
			actList.addAll(dispatchList);
		}
		if(!covered && actList.size() > 0) {//不覆盖，则需进行数据校验，剔除人工修改的数据
			List<ProtectCorrectAct> proList = new ArrayList<ProtectCorrectAct>();
			if(month == null) {
				proList = dao.findByCondition(ProtectCorrectAct.class, "year=? and month is null and status = 1", year);
			}else {
				if(month == 0) {
					proList = dao.findByCondition(ProtectCorrectAct.class, "year=? and month is not null and status = 1", year);
				}else {
					proList = dao.findByCondition(ProtectCorrectAct.class, "year=? and month=? and status = 1", year, month);
				}
			}
			List<String> keyList = new ArrayList<String>();//key数据列表 单位年月电压等级
			for(ProtectCorrectAct pro : proList) {
				String key = pro.getSubCompany() + pro.getYear() + pro.getMonth() + pro.getVoltage();
				keyList.add(key);
			}
			Iterator<ProtectCorrectAct> actIt = actList.iterator();
			while(actIt.hasNext()) {//剔除人工修改的数据
				ProtectCorrectAct itNext = actIt.next();
				String key = itNext.getSubCompany() + itNext.getYear() + itNext.getMonth() + itNext.getVoltage();
				if(keyList.contains(key)) {
					actIt.remove();
				}
			}
		}
		try {
			dao.persistentBatch(ProtectCorrectAct.class, actList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据所给单位保护动作次数数据，计算保护动作正确率
	 * @param unitMap <单位，动作数据列表>
	 * @param unitType 单位类型（0：检修单位，1：调管单位）
	 * @param year
	 * @param month
	 * @param voltage
	 * @return
	 */
	private List<ProtectCorrectAct> countProtectCorrectAct(Map<String, List<ProtectCorrectActInner>> unitMap, int unitType, Integer year, Integer month, Integer voltage) {
		List<ProtectCorrectAct> actList = new ArrayList<ProtectCorrectAct>();
		for(Map.Entry<String, List<ProtectCorrectActInner>> entry : unitMap.entrySet()) {
			List<ProtectCorrectActInner> proList = entry.getValue();
			if(null != proList) {
				int totalActSum = 0;
				int unwantedActSum = 0;//误动次数
				int refuseActSum = 0;//拒动次数
				int rightActSum = 0;//正确动作次数
				for (ProtectCorrectActInner pro : proList) {
					int protectUnwantedActionCount = pro.getProtectUnwantedActionCount().intValue();
					int protectRefuseActionCount = pro.getProtectRefuseActionCount().intValue();
					int protectRightActionCount = pro.getProtectRightActionCount().intValue();
					unwantedActSum += protectUnwantedActionCount;
					refuseActSum += protectRefuseActionCount;
					rightActSum += protectRightActionCount;
					year = pro.getYear();
					month = pro.getMonth();
					voltage = pro.getVoltage();
				}
				totalActSum = unwantedActSum + refuseActSum + rightActSum;
				ProtectCorrectAct act = new ProtectCorrectAct();
				act.setId(ObjectId.get().toString());
				act.setYear(year);
				act.setMonth(month);
				act.setVoltage(voltage);
				act.setPlan(100f);
				act.setActNum(totalActSum);
				act.setUnwantedActNum(unwantedActSum + refuseActSum);//不正确动作次数=误动次数+拒动次数
				act.setStatus(0);
				if(totalActSum!=0) {
					String reality = df.format((float)(rightActSum)/totalActSum*100);//保护正确动作率=正确动作次数/总动作次数
					act.setReality(Float.parseFloat(reality));
				} else {
					act.setReality(100f);
				}
				act.setSubCompany(entry.getKey());
				act.setUnitType(unitType);
				actList.add(act);
			}
		}
		return actList;
	}

	/**
	 * 统计所有的保护正确动作率
	 * @return
	 */
	public void countProtectCorrectAct() {
		List<ProtectCorrectAct> actList = new ArrayList<ProtectCorrectAct>();
		String sql = ALL_COUNT_ACT_SQL;
		List<ProtectCorrectActInner> list = dao.nativeQuery(ProtectCorrectActInner.class, sql, null);
		Map<String, List<ProtectCorrectActInner>> maintenanceUnitMap = commonM.generateProtectDispatchUnitMap(list);
		List<ProtectCorrectAct> maintenanceList = countProtectCorrectActTwo(maintenanceUnitMap, 0);
		actList.addAll(maintenanceList);
		Map<String, List<ProtectCorrectActInner>> dispatchUnitMap = commonM.generateProtectDispatchUnitMap(list);
		List<ProtectCorrectAct> dispatchList = countProtectCorrectActTwo(dispatchUnitMap, 1);
		actList.addAll(dispatchList);
		try {
			dao.persistentBatch(ProtectCorrectAct.class, actList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据所给单位保护动作次数数据，计算保护动作正确率
	 * @param unitMap <年-月-调管单位-电压等级，动作数据列表>
	 * @param unitType 单位类型（0：检修单位，1：调管单位）
	 * @return
	 */
	private List<ProtectCorrectAct> countProtectCorrectActTwo(Map<String, List<ProtectCorrectActInner>> unitMap, int unitType) {
		List<ProtectCorrectAct> actList = new ArrayList<ProtectCorrectAct>();
		for(Map.Entry<String, List<ProtectCorrectActInner>> entry : unitMap.entrySet()) {
			List<ProtectCorrectActInner> proList = entry.getValue();
			if(null != proList) {
				int totalActSum = 0;
				int unwantedActSum = 0;//误动次数
				int refuseActSum = 0;//拒动次数
				int rightActSum = 0;//正确动作次数
				for (ProtectCorrectActInner pro : proList) {
					int protectUnwantedActionCount = pro.getProtectUnwantedActionCount().intValue();
					int protectRefuseActionCount = pro.getProtectRefuseActionCount().intValue();
					int protectRightActionCount = pro.getProtectRightActionCount().intValue();
					unwantedActSum += protectUnwantedActionCount;
					refuseActSum += protectRefuseActionCount;
					rightActSum += protectRightActionCount;
				}
				totalActSum = unwantedActSum + refuseActSum + rightActSum;
				ProtectCorrectAct act = new ProtectCorrectAct();
				act.setId(ObjectId.get().toString());
				String[] keys = entry.getKey().split("-");
				act.setYear(Integer.parseInt(keys[0]));
				act.setMonth(Integer.parseInt(keys[1]));
				act.setVoltage(Integer.parseInt(keys[3]));
				act.setPlan(100f);
				act.setActNum(totalActSum);
				act.setUnwantedActNum(unwantedActSum + refuseActSum);//不正确动作次数=误动次数+拒动次数
				act.setStatus(0);
				if(totalActSum!=0) {
					String reality = df.format((float)(rightActSum)/totalActSum*100);//保护正确动作率=正确动作次数/总动作次数
					act.setReality(Float.parseFloat(reality));
				} else {
					act.setReality(100f);
				}
				act.setSubCompany(keys[2]);
				act.setUnitType(unitType);
				actList.add(act);
			}
		}
		return actList;
	}
	/**
	 * 根据年、月、电压等级统计故障快速切除率
	 * @param year
	 * @param month
	 * @param voltage
	 */
	@Deprecated
	public void countFaultFastRemove(Integer year,Integer month,Integer voltage){
		List<FaultFastRemove> removeList = new ArrayList<FaultFastRemove>();
		String sql = (month==null?YEAR_COUNT_REMOVE_SQL:MONTH_COUNT_REMOVE_SQL);
		List<?> list = commonM.getListByCondition(year,month,voltage,sql);
		Map<String, List<Object>> companyMap = commonM.getCompanyMap(list, 0);
		for (String company : companyMap.keySet()) {
			List<Object> compList = companyMap.get(company);
			if(null != compList) {
				int actionReality = 0;
				int actionShould = 0;
				for (Object obj : compList) {
					int rapidActionReality = 0;
					int rapidProtectionShould = 0;
					Object[] objs = (Object[])obj;
					if(null != objs[1]) {
						//						Double reality = (Double) objs[1];
						rapidActionReality = objectTransToInteger(objs[1]);
					}
					if(null != objs[2]) {
						//						Double should = (Double) objs[2];
						rapidProtectionShould = objectTransToInteger(objs[2]);
					}
					actionReality += rapidActionReality;
					actionShould += rapidProtectionShould;
				}
				FaultFastRemove ffr = new FaultFastRemove();
				ffr.setId(ObjectId.get().toString());
				ffr.setYear(year);
				ffr.setMonth(month);
				ffr.setVoltage(voltage);
				ffr.setPlan(100f);
				ffr.setActNum(actionReality);
				ffr.setWantedActNum(actionShould);
				ffr.setStatus(0);
				if(actionReality!=0) {
					String reality = df.format((float)actionShould/actionReality*100);
					ffr.setReality(Float.parseFloat(reality));
				} else {
					ffr.setReality(100f);
				}
				ffr.setSubCompany(company);
				removeList.add(ffr);
			}
		}
		try {
			dao.persistentBatch(FaultFastRemove.class, removeList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据年、月、电压等级统计故障快速切除率
	 * @param year
	 * @param month
	 * @param voltage
	 */
	public void countFaultFastRemove(Integer year, Integer month, Integer voltage, int unitType, boolean covered){
		List<FaultFastRemove> removeList = new ArrayList<FaultFastRemove>();
		String sql = (month==null?YEAR_COUNT_REMOVE_SQL:MONTH_COUNT_REMOVE_SQL);
		List<FaultFastRemoveInner> list = commonM.getRemoveByConditions(year, month, voltage, sql);
		if(unitType == 0) {
			Map<String, List<FaultFastRemoveInner>> maintenanceUnitMap = commonM.getRemoveMaintenanceUnitMap(list);//获取检修单位对应的数据map
			List<FaultFastRemove> maintenanceList = countFaultFastRemove(maintenanceUnitMap, 0, year,  month,  voltage);
			removeList.addAll(maintenanceList);
		}else {
			Map<String, List<FaultFastRemoveInner>> dispatchUnitMap = commonM.getRemoveDispatchUnitMap(list);//获取调管单位对应的数据map
			List<FaultFastRemove> dispatchList = countFaultFastRemove(dispatchUnitMap, 1, year,  month,  voltage);
			removeList.addAll(dispatchList);
		}
		if(!covered) {//不覆盖，则需进行数据校验，剔除人工修改的数据
			List<FaultFastRemove> fasList = new ArrayList<FaultFastRemove>();
			if(month == null) {
				fasList = dao.findByCondition(FaultFastRemove.class, "year=? and month is null and status = 1", year);
			}else {
				if(month == 0) {
					fasList = dao.findByCondition(FaultFastRemove.class, "year=? and month is not null and status = 1", year);
				}else {
					fasList = dao.findByCondition(FaultFastRemove.class, "year=? and month=? and status = 1", year, month);
				}
			}
			List<String> keyList = new ArrayList<String>();//key数据列表 单位年月电压等级
			for(FaultFastRemove fas : fasList) {
				String key = fas.getSubCompany() + fas.getYear() + fas.getMonth() + fas.getVoltage();
				keyList.add(key);
			}
			Iterator<FaultFastRemove> fasIt = removeList.iterator();
			while(fasIt.hasNext()) {//剔除人工修改的数据
				FaultFastRemove itNext = fasIt.next();
				String key = itNext.getSubCompany() + itNext.getYear() + itNext.getMonth() + itNext.getVoltage();
				if(keyList.contains(key)) {
					fasIt.remove();
				}
			}
		}
		try {
			dao.persistentBatch(FaultFastRemove.class, removeList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据所给单位故障快速切除次数数据，计算故障快速切除率
	 * @param unitMap <单位，动作数据列表>
	 * @param unitType 单位类型（0：检修单位，1：调管单位）
	 * @param year
	 * @param month
	 * @param voltage
	 * @return
	 */
	private List<FaultFastRemove> countFaultFastRemove(Map<String, List<FaultFastRemoveInner>> unitMap, int unitType, Integer year, Integer month, Integer voltage) {
		List<FaultFastRemove> removeList = new ArrayList<FaultFastRemove>();
		for(Map.Entry<String, List<FaultFastRemoveInner>> entry : unitMap.entrySet()) {
			List<FaultFastRemoveInner> fastList = entry.getValue();
			if(null != removeList) {
				int actionReality = 0;
				int actionShould = 0;
				for(FaultFastRemoveInner fast : fastList) {
					int rapidActionReality = fast.getRapidActionReality().intValue();
					int rapidProtectionShould = fast.getRapidProtectionShould().intValue();
					actionReality += rapidActionReality;
					actionShould += rapidProtectionShould;
					year = fast.getYear();
					month = fast.getMonth();
					voltage = fast.getVoltage();
				}
				FaultFastRemove ffr = new FaultFastRemove();
				ffr.setId(ObjectId.get().toString());
				ffr.setYear(year);
				ffr.setMonth(month);
				ffr.setVoltage(voltage);
				ffr.setPlan(100f);
				ffr.setActNum(actionReality);
				ffr.setWantedActNum(actionShould);
				ffr.setStatus(0);
				if(actionReality!=0) {
					String reality = df.format((float)actionReality/actionShould*100);
					ffr.setReality(Float.parseFloat(reality));
				} else {
					ffr.setReality(100f);
				}
				ffr.setSubCompany(entry.getKey());
				ffr.setUnitType(unitType);
				removeList.add(ffr);
			}
		}
		return removeList;
	}

	/**
	 * 统计所有的故障快速切除率
	 */
	public void countFaultFastRemove(){
		List<FaultFastRemove> removeList = new ArrayList<FaultFastRemove>();
		String sql = ALL_COUNT_REMOVE_SQL;
		List<FaultFastRemoveInner> list = dao.nativeQuery(FaultFastRemoveInner.class, sql, null);
		Map<String, List<FaultFastRemoveInner>> maintenanceUnitMap = commonM.generateRemoveDispatchUnitMap(list);
		List<FaultFastRemove> maintenanceList = countFaultFastRemoveTwo(maintenanceUnitMap, 0);
		removeList.addAll(maintenanceList);
		Map<String, List<FaultFastRemoveInner>> dispatchUnitMap = commonM.generateRemoveDispatchUnitMap(list);
		List<FaultFastRemove> dispatchList = countFaultFastRemoveTwo(dispatchUnitMap, 1);
		removeList.addAll(dispatchList);
		try {
			dao.persistentBatch(FaultFastRemove.class, removeList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据所给单位故障快速切除次数数据，计算保护动作正确率
	 * @param unitMap <年-月-调管单位-电压等级，动作数据列表>
	 * @param unitType 单位类型（0：检修单位，1：调管单位）
	 * @return
	 */
	public List<FaultFastRemove> countFaultFastRemoveTwo(Map<String, List<FaultFastRemoveInner>> unitMap, int unitType){
		List<FaultFastRemove> removeList = new ArrayList<FaultFastRemove>();
		for(Map.Entry<String, List<FaultFastRemoveInner>> entry : unitMap.entrySet()) {
			List<FaultFastRemoveInner> fastList = entry.getValue();
			if(null != fastList) {
				int actionReality = 0;
				int actionShould = 0;
				for (FaultFastRemoveInner fast : fastList) {
					int rapidActionReality = fast.getRapidActionReality().intValue();
					int rapidProtectionShould = fast.getRapidProtectionShould().intValue();
					actionReality += rapidActionReality;
					actionShould += rapidProtectionShould;
				}
				FaultFastRemove ffr = new FaultFastRemove();
				ffr.setId(ObjectId.get().toString());
				String[] keys = entry.getKey().split("-");
				ffr.setYear(Integer.parseInt(keys[0]));
				ffr.setMonth(Integer.parseInt(keys[1]));
				ffr.setVoltage(Integer.parseInt(keys[3]));
				ffr.setPlan(100f);
				ffr.setActNum(actionReality);
				ffr.setWantedActNum(actionShould);
				ffr.setStatus(0);
				if(actionReality!=0) {
					String reality = df.format((float)actionReality/actionShould*100);
					ffr.setReality(Float.parseFloat(reality));
				} else {
					ffr.setReality(100f);
				}
				ffr.setSubCompany(keys[2]);
				ffr.setUnitType(unitType);
				removeList.add(ffr);
			}
		}
		return removeList;
	}

	private int objectTransToInteger(Object obj) {
		String str = obj.toString();
		Double d = Double.parseDouble(str);
		return d.intValue();
	}

	/**
	 * 保护正确动作率统计相关信息
	 *
	 */
	public static class ProtectCorrectActInner{
		/**
		 * 站id
		 */
		private String stationId;
		/**
		 * 年
		 */
		private Integer year;
		/**
		 * 月
		 */
		private Integer month;
		/**
		 *
		 */
		private Integer voltage;
		/**
		 * 正确动作次数
		 */
		private Long protectRightActionCount;
		/**
		 * 拒动次数
		 */
		private Long protectRefuseActionCount;
		/**
		 * 误动次数
		 */
		private Long protectUnwantedActionCount;

		public String getStationId() {
			return stationId;
		}

		public void setStationId(String stationId) {
			this.stationId = stationId;
		}

		public Integer getYear() {
			return year;
		}

		public void setYear(Integer year) {
			this.year = year;
		}

		public Integer getMonth() {
			return month;
		}

		public void setMonth(Integer month) {
			this.month = month;
		}

		public Integer getVoltage() {
			return voltage;
		}

		public void setVoltage(Integer voltage) {
			this.voltage = voltage;
		}

		public Long getProtectRightActionCount() {
			return protectRightActionCount;
		}

		public void setProtectRightActionCount(Long protectRightActionCount) {
			this.protectRightActionCount = protectRightActionCount;
		}

		public Long getProtectRefuseActionCount() {
			return protectRefuseActionCount;
		}

		public void setProtectRefuseActionCount(Long protectRefuseActionCount) {
			this.protectRefuseActionCount = protectRefuseActionCount;
		}

		public Long getProtectUnwantedActionCount() {
			return protectUnwantedActionCount;
		}

		public void setProtectUnwantedActionCount(Long protectUnwantedActionCount) {
			this.protectUnwantedActionCount = protectUnwantedActionCount;
		}

	}

	/**
	 * 故障快速切除率统计相关信息
	 *
	 */
	public static class FaultFastRemoveInner{
		/**
		 * 站id
		 */
		private String stationId;
		/**
		 * 年
		 */
		private Integer year;
		/**
		 * 月
		 */
		private Integer month;
		/**
		 *
		 */
		private Integer voltage;
		/**
		 * 故障快速切除次数
		 */
		private Long rapidActionReality;
		/**
		 * 应评价故障快速切除次数
		 */
		private Long rapidProtectionShould;

		public String getStationId() {
			return stationId;
		}

		public void setStationId(String stationId) {
			this.stationId = stationId;
		}

		public Integer getYear() {
			return year;
		}

		public void setYear(Integer year) {
			this.year = year;
		}

		public Integer getMonth() {
			return month;
		}

		public void setMonth(Integer month) {
			this.month = month;
		}

		public Integer getVoltage() {
			return voltage;
		}

		public void setVoltage(Integer voltage) {
			this.voltage = voltage;
		}

		public Long getRapidActionReality() {
			return rapidActionReality;
		}

		public void setRapidActionReality(Long rapidActionReality) {
			this.rapidActionReality = rapidActionReality;
		}

		public Long getRapidProtectionShould() {
			return rapidProtectionShould;
		}

		public void setRapidProtectionShould(Long rapidProtectionShould) {
			this.rapidProtectionShould = rapidProtectionShould;
		}

	}

}
